<?php

class FixImagesController extends Model3_Controller {

    function __construct() 
    {
    }
    
    function graphBarsAction($GraphData, $GraphLabel) 
    {
    //Metemos la grafica con puro php jpeg BAR graph
    $GraphWidth = 400;
    $GraphHeight = 200;
    $GraphFont = 2;

    //creando la imagen
    $image = imagecreate($GraphWidth, $GraphHeight);
    
    //allocate colors
    $colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
    $colorGrid = imagecolorallocate($image, 0xCC, 0xCC, 0xCC);
    $colorBar0 = imagecolorallocate($image, 0xCC, 0x00, 0x00);
    $colorBar1 = imagecolorallocate($image, 0x26, 0x7B, 0xC6);
    $colorBar2 = imagecolorallocate($image, 0xB0, 0xDE, 0x27);
    $colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);
    //fill bacground
    imagefill($image, 0, 0, $colorBody);

    //draw vertical grid line
    $GridLabelWidth = imagefontwidth(($GraphFont) * 3 + 1);
    imageline($image, $GridLabelWidth, 0, $GridLabelWidth, $GraphHeight - 1, $colorGrid);

    //draw horizontal grid lines 
    $GraphScale = 2;
    for ($index = 0; $index < $GraphHeight; $index += $GraphHeight / 10) 
    {
        imagedashedline($image, 0, $index, $GraphWidth - 1, $index, $colorGrid);
        //draw label
        imagestring($image, $GraphFont, 0, $index, round(($GraphHeight - $index) / $GraphScale).'%', $colorText);
    }
    //add botton line
    imageline($image, 0, $GraphHeight - 1, $GraphWidth - 1, $GraphHeight - 1, $colorGrid);

    //draw each bar
    $BarWidth = (($GraphWidth - $GridLabelWidth) / count($GraphData)) - 10;
    for ($index = 0; $index < count($GraphData); $index++) 
    {
        //draw bar
        $BarTopX = $GridLabelWidth + (($index + 1) * 10) + ($index * $BarWidth);
        $BarBottomX = $BarTopX + $BarWidth;
        $BarBottomY = $GraphHeight - 1;
        $BarTopY = $BarBottomY - ($GraphData[$index] * .00275);
        switch($index)
        {
            case 1:
                $colorBar = $colorBar1;
            break;
            case 2:
                $colorBar = $colorBar2;
            break;
            default:
                $colorBar = $colorBar0;
        }
        imagefilledrectangle($image, $BarTopX, $BarTopY, $BarBottomX, $BarBottomY, $colorBar);

        //draw label
        $labelX = $BarTopX + (($BarBottomX - $BarTopX) / 2) - (imagefontheight($GraphFont) / 2);
        $labelY = $BarTopY - 10;

        imagestringup($image, $GraphFont, $labelX, $labelY, $GraphLabel[$index], $colorText);
    }
// salida para un img 
//      imagettftext($image, 50, 0, 0, 0, $colorBar, $GraphFont, "Graphic");
//output image
//    header("Content-type: image/gif");
        $url = "images/Graficas/graficaBarras.png";
        imagepng($image, $url);
        $ruta = "SalonEventos/public/images/Graficas/graficaBarras.png";
        imagedestroy($image);
        return($url);
    }
    
    function graphPieAction($ChartData, $ChartLabel)
    {
        //fill in chart parameters 
        $ChartDiameter = 300;
        $ChartFont = 4;
        $ChartFontHeight = imagefontheight($ChartFont);
       
        
        //determine graphic size
        $ChartWidth = $ChartDiameter + 20;
        $ChartFontHeight = $ChartDiameter + 20 + (($ChartFontHeight + 2) * count($ChartData));
        
        //determine total of all values
        $ChartTotal = 0;
        for($index = 0; $index <count($ChartData); $index++)
        {
            $ChartTotal += $ChartData[$index]; 
        }
        $ChartCenterX = $ChartDiameter/2 + 10;
        $ChartCenterY = $ChartDiameter/2 + 10;
        
        //create image
        $image = imagecreate($ChartWidth, $ChartWidth);
        
        //allocate colors
        $colorBody = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
        $colorBorder = imagecolorallocate($image, 0x00, 0x00, 0x00);
        $colorText = imagecolorallocate($image, 0x00, 0x00, 0x00);
        
        $colorSlice[] = imagecolorallocate($image, 0x00, 0xFF, 0x00); //verde
        $colorSlice[] = imagecolorallocate($image, 0x00, 0x00, 0xFF); //azul
        $colorSlice[] = imagecolorallocate($image, 0xFF, 0x00, 0x00); //rojo
        $colorSlice[] = imagecolorallocate($image, 0xFF, 0xFF, 0x00);
        $colorSlice[] = imagecolorallocate($image, 0xFF, 0x00, 0xFF);
        $colorSlice[] = imagecolorallocate($image, 0x00, 0xFF, 0xFF);
        $colorSlice[] = imagecolorallocate($image, 0x99, 0x00, 0x00);
        $colorSlice[] = imagecolorallocate($image, 0x00, 0x99, 0x00);
        $colorSlice[] = imagecolorallocate($image, 0x00, 0x00, 0x99);
        $colorSlice[] = imagecolorallocate($image, 0x99, 0x99, 0x00);
        $colorSlice[] = imagecolorallocate($image, 0x99, 0x00, 0x99);
        $colorSlice[] = imagecolorallocate($image, 0x00, 0x99, 0x99);
        
        //fill background
        imagefill($image, 0, 0, $colorBody);
        
        /*
        ** Draw each slice
        */
        $Degrees = 0;
        for($index = 0; $index < count($ChartData); $index++)
        {
            $StartDegrees = round($Degrees);
            $Degrees += (($ChartData[$index]/$ChartTotal) * 360);
            $EndDegrees = round($Degrees);
            $CurrentColor = $colorSlice[$index%(count($colorSlice))];
            
            //draw arc
            imagearc($image, 
                    $ChartCenterX, 
                    $ChartCenterY, 
                    $ChartDiameter,                   
                    $ChartDiameter, 
                    $StartDegrees, 
                    $EndDegrees, 
                    $CurrentColor);
            
            //draw start line from center
            list($ArcX,$ArcY) = $this->circle_point($StartDegrees, $ChartDiameter);
            imageline($image, 
                    $ChartCenterX, 
                    $ChartCenterY, 
                    floor($ChartCenterX + $ArcX),
                    floor($ChartCenterY + $ArcY),
                    $CurrentColor);
            
            //draw end line from center 
            list($ArcX,$ArcY) = $this->circle_point($EndDegrees, $ChartDiameter);
            imageline($image, 
                    $ChartCenterX, 
                    $ChartCenterY, 
                    ceil($ChartCenterX + $ArcX),
                    ceil($ChartCenterY + $ArcY),
                    $CurrentColor);
            
            //fill slice
            $MidPoint = round((($EndDegrees - $StartDegrees)/ 2)+ $StartDegrees);
            list($ArcX,$ArcY) = $this->circle_point($MidPoint, $ChartDiameter/2);
            imagefilltoborder($image, 
                    floor($ChartCenterX + $ArcX),
                    floor($ChartCenterY + $ArcY), 
                    $CurrentColor,
                    $CurrentColor);
        }
        
        //draw border 
        imagearc($image, 
                $ChartCenterX , 
                $ChartCenterY, 
                $ChartDiameter, 
                $ChartDiameter,
                0,
                360, 
                $colorBorder);
        
        imagearc($image,
                $ChartCenterX , 
                $ChartCenterY, 
                $ChartDiameter+7, 
                $ChartDiameter+7, 
                0,
                360, 
                $colorBorder);
        
        imagefilltoborder($image, 
                floor($ChartCenterX + ($ChartDiameter/2) + 2), 
                $ChartCenterY, 
                $colorBorder,
                $colorBorder);
        
        //draw legend
        for($index = 0; $index < count($ChartData); $index++)
        {
            $CurrentColor = $colorSlice[$index%(count($colorSlice))];
            $LineY = $ChartDiameter + 20 + ($index*($ChartFontHeight + 2));
            
            //draw color box
            imagerectangle($image, 
                    10, 
                    $LineY, 
                    10 + $ChartFontHeight, 
                    $LineY + $ChartFontHeight, 
                    $colorBorder);
            
//            //fill color border
//            imagefilltoborder($image, 
//                    11, 
//                    $LineY + 1, 
//                    $colorBorder, 
//                    $CurrentColor);
            
//            //draw label
//            imagestring($image, 
//                    $ChartFont, 
//                    70 + ($ChartDiameter/3 * $index), 
//                    50 + ($ChartDiameter/4 * $index), 
//                    "$$ChartData[$index]", 
//                    $colorText);
        }
        
        $url = "images/Graficas/graficaPastel.png";
        imagepng($image, $url);
        imagedestroy($image);
        return($url);
    }
    
    /**
     * Convertir de grados a radianes
     * @param type $degrees
     */
    function radians($degrees)
    {
        return ($degrees * (pi()/180.0));
    }
    
    function circle_point($degrees, $diameter)
    {
        $x = cos($this->radians($degrees))*($diameter/2);
        $y = sin($this->radians($degrees))*($diameter/2);
        
        return (array($x,$y));
    }
    
}